<template>
  <div class='sub-sort'>
    <div class="sort">
      <a href="javascript:;" @click="changeSort(null)"  :class="{active: sortField == null}" >默认排序</a>  
      <a href="javascript:;" @click="changeSort('publishTime')" :class="{active: sortField == 'publishTime'}"  >最新商品</a>
      <a href="javascript:;" @click="changeSort('orderNum')"  :class="{active: sortField == 'orderNum'}">最高人气</a>
      <a href="javascript:;" @click="changeSort('evaluateNum')"  :class="{active: sortField == 'evaluateNum'}">评论最多</a>
      <a href="javascript:;" @click="changeSort('price')">
        价格排序
        <i class="arrow up" :class="{active:sortField==='price'&&sortMethod=='asc'}" />
        <i class="arrow down" :class="{ active:sortField === 'price'&&sortMethod == 'desc' } " />
      </a>
    </div>
    <div class="check">
      <el-checkbox-group v-model="showGoods" fill="#fff" @change="handlerCheckbox">
        <el-checkbox label="inventory" :key="1">仅显示有货商品</el-checkbox>
        <el-checkbox label="onlyDiscount">仅显示特惠商品</el-checkbox>
      </el-checkbox-group>
    </div>
  </div>
</template>
<script>
import { reactive, toRefs, ref } from 'vue'
export default {
  name: 'SubSort',
  setup(props,{emit}){
    const sortParams = reactive({
      sortField: null,
      sortMethod: null,
      inventory:null,
      onlyDiscount:null
    })
    const showGoods = ref([])
    const handlerCheckbox = (varArr) => {
      sortParams.inventory = varArr.indexOf('inventory') !== -1 ?  true : null
      sortParams.onlyDiscount = varArr.indexOf('onlyDiscount') !== -1 ?  true : null
      emit('filter-change',sortParams)
    }
    const changeSort = (sortId)=>{
      if(sortId === 'price'){
        sortParams.sortField = sortId
        if(sortParams.sortMethod === null){
          sortParams.sortMethod = 'desc'
        }else{
          sortParams.sortMethod = sortParams.sortMethod === 'desc' ? 'asc' : 'desc'
        }
        emit('filter-change',sortParams)
      }else{
        if (sortParams.sortField === sortId)  return
        sortParams.sortField = sortId
        sortParams.sortMethod = null
        emit('filter-change',sortParams)
      }
    }
    return {
      ...toRefs(sortParams),
      handlerCheckbox,
      changeSort,
      showGoods
    }
  }
}
</script>
<style scoped lang='scss'>
/* 选中后的字体颜色 */
::v-deep .el-checkbox__input.is-checked+.el-checkbox__label {
  color:$xtxColor !important;
}
::v-deep .el-checkbox__input.is-checked .el-checkbox__inner{
  background: $xtxColor;
}
.sub-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  .sort {
    display: flex;
    a {
      height: 30px;
      line-height: 28px;
      border: 1px solid #e4e4e4;
      padding: 0 20px;
      margin-right: 20px;
      color: #999;
      border-radius: 2px;
      position: relative;
      transition: all .3s;
      &.active {
        background: $xtxColor;
        border-color: $xtxColor;
        color: #fff;
      }
      .arrow {
        position: absolute;
        border: 5px solid transparent;
        right: 8px;
        &.up {
          top: 3px;
          border-bottom-color: #bbb;
            &.active {
            border-bottom-color: $xtxColor;
          }
        }
        &.down {
          top: 15px;
          border-top-color: #bbb;
          &.active {
            border-top-color: $xtxColor;
          }
        }
      }
    }
  }
  .check {
    .xtx-checkbox {
      margin-left: 20px;
      color: #999;
    }
  }
}
</style>